<?php
include_once('config/config.php');
$auth = check_auth();
$action = $_GET['action'] ?? '';
include_once('./config/logout.php');
if ($_GET) {
	$id= $_GET['id'] ?? '';
	$jobSql = "SELECT * FROM `pre_job` WHERE `id` = '$id'";
	// var_dump($id);
	$jobData = find($jobSql);
	// var_dump($jobData);
}else{
	showMsg('该管理员不存在');
}
// 通过sql语句查询部门名称添加到选择框中
$finsdepidSql = "SELECT * FROM `pre_dep`";
$depidData = finds($finsdepidSql);
// var_dump($depidData);
// exit;
// 判断是否接收post
if ($_POST) {
	// 接收post函数
	$jobname = $_POST['jobname'] ?? '';
	$depid = $_POST['depid'] ?? '';
	// var_dump($depid);
	// exit;
	// 判断输入框是否为空
	if (empty($jobname)) {
		showMsg('职位名称必填');
	}
	// 查询部门是否存在
	$depSql = "SELECT * FROM `pre_dep` WHERE `id` = '$depid'";

	$depInfo = find($depSql);

	if (!$depInfo) {
		showMsg('部门不存在');
	}
	// 通过sql语句查询职位名是否存在
	$jobSql = "SELECT * FROM `pre_job` WHERE `name` = '$jobname'";
	$jobData = find($jobSql);
	// if ($jobData) {
	// 	showMsg('职位名已存在');
	// }
	$data = [
		'name' => $jobname,
		'depid' => (int)$depid
	];
	$where="`id` = '$id'";
	$result = update('job', $data , $where);
	if ($result['code'] === 1) {
		showMsg('修改职位成功', 'jobList.php');
	} else {
		// 输出错误信息
		echo $result['msg'];
		exit;
	}
}
?>
<!DOCTYPE html>
<html lang="en">
<style>
	/* .row-fluid [class*="span"]{
    margin-left: 0;
  } */
</style>

<head>
	<?php include_once('common/meta.php'); ?>
</head>
<link rel="stylesheet" href="./assets/lib/city-picker/city-picker.css">

<body>
	<?php include_once('common/header.php'); ?>
	<?php include_once('common/menu.php'); ?>
	<div class="content">
		<div class="header">
			<h1 class="page-title">添加职位</h1>
		</div>
		<ul class="breadcrumb">
			<li><a href="index.html">Home</a> <span class="divider">/</span></li>
			<li class="active">Index</li>
		</ul>

		<div class="container-fluid">
			<div class="row-fluid">

				<div class="btn-toolbar">
					<button class="btn btn-primary" onClick="location='list.html'"><i class="icon-list"></i> 职位列表</button>
					<div class="btn-group">
					</div>
				</div>

				<div class="well">
					<div id="myTabContent" class="tab-content">
						<div class="tab-pane active in" id="home">
							<form method="post">

								<label>职位名称</label>
								<input type="text" id="jobname" name="jobname" class="input-xxlarge">

								<label>部门</label>
								<select name="depid" id="depid" class="input-xlarge">
									<option value="0">请选择</option>
									<?php foreach ($depidData as $item) {
										$id = $item['id'];
										echo "<option value='$id'>" . $item['name'] . "</option>";
									}
									?>
								</select>

								<label></label>
								<input class="btn btn-primary" type="submit" value="提交" />
							</form>
						</div>
					</div>
				</div>

				<div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
						<h3 id="myModalLabel">Delete Confirmation</h3>
					</div>
					<div class="modal-body">

						<p class="error-text"><i class="icon-warning-sign modal-icon"></i>Are you sure you want to delete the user?</p>
					</div>
					<div class="modal-footer">
						<button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button>
						<button class="btn btn-danger" data-dismiss="modal">Delete</button>
					</div>
				</div>

				<?php include_once('common/footer.php') ?>

			</div>
		</div>
	</div>
</body>

</html>
<?php include_once('common/script.php') ?>
<script src="./assets/lib/city-picker/city-picker.data.min.js"></script>
<script src="./assets/lib/city-picker/city-picker.min.js"></script>
<script>
	// 模拟点击上传框
	$('#upload').click(function() {
		$('#avatar').click();
	});

	// 监听上传框的值是否有变化
	$('#avatar').change(function() {
		let file = $(this)[0].files[0];

		if (file) {
			let Reader = new FileReader();

			Reader.readAsDataURL(file);

			Reader.onload = function() {
				$('#upload').attr('src', Reader.result);
			}
		}
	});
	$('#jobname').val('<?php echo $jobData['name']; ?>');
	$('#depid').val('<?php echo $jobData['depid']; ?>');

</script>